<template>
  <div class="login-page">
    <el-card>
      <template #header>
        面经运营后台
      </template>
      <!--
      表单校验三要素：
      1、el-form：必须要有 model 属性和 rules 属性
        - model：表示当前表单绑定的数据对象
        -rules：规则，校验规则
      2、e-form-item：必须要有 prop 属性来指定校验规则
      3、el-input：绑定 v-model
      -->
      <el-form ref="form" :model="form" :rules="rules" >

        <el-form-item label="用户名：" prop="username">
          <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="密码："  prop="password">
          <el-input placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>

      </el-form>
    </el-card>
  </div>
</template>

<script >
export default {
  name: 'LoginIndex',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        // username是prop指定的名字，一般情况下建议：
        // 数据 - prop - 规则名 三者保持一致
        username: [
          // 数组，可以存入多条规则
          // required:必填型，message：提示信息，trigger：blur失去焦点，校验
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{4,10}$/, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // try {
        await this.$store.dispatch('user/loginAction', this.form)
        // 跳转到首页
        this.$router.push('/')
        this.$message.success('登陆成功')
        // } catch (e) {
        //   this.$message.error(e.response.data.message)
        // }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
 .login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header{
      height: 80px;
      background: rgba(114,124,245,1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
